﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlatRedBall;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework.Graphics;

namespace TopDownShooter
{
    class HealthBar : PositionedObject
    {
        AxisAlignedRectangle Container;
        AxisAlignedRectangle Progress;
        public HealthBar()
        {
            Container = ShapeManager.AddAxisAlignedRectangle();
            Progress = ShapeManager.AddAxisAlignedRectangle();
            Container.ScaleX = 2;
            Container.AttachTo(this, false);
            Progress.AttachTo(this, false);
            Progress.ScaleX = 0;

            Container.ScaleY = .2f;
            Progress.ScaleY = .2f;

            ParentRotationChangesPosition = false;
            Container.ParentRotationChangesPosition = false;
            Progress.ParentRotationChangesPosition = false;
        }
        public void SetHealth(int health)
        {
            if (health < 0)
                health = 0;
            float fraction = (health / 100f) * 2;
            Progress.ScaleX = fraction;
            Progress.RelativeX = fraction - Container.ScaleX;
            UpdateColor(health);
        }

        private void UpdateColor(int health)
        {
            if (health > 50)
                Progress.Color = Color.Green;
            else if (health <= 50 && health >= 25)
                Progress.Color = Color.Yellow;
            else
                Progress.Color = Color.Red;
        }

        internal void Destroy()
        {
            ShapeManager.Remove(Container);
            ShapeManager.Remove(Progress);
            RemoveSelfFromListsBelongingTo();
        }
    }
}
